<?php

	class loginAction extends Action {
		
		public function index(){
			$this->display("login");
		}

		public function login(){
			$this->display();
		}
		
		//登录检测
		public function checklogin(){
			$userDao = D("user");
			$userEmail = isset($_POST["email"]) ? $_POST["email"] : Cookie::get("email");
			$userPassword = isset($_POST["password"]) ? md5($_POST["password"]) : Cookie::get("password");
			$userInfo = $userDao->find("email='".$userEmail."' and password='".$userPassword."'","id,username,network_city,faceimg");
			if($userInfo){
				//更新登录时间
				$userDao->setField("logintime",time(),"id=".$userInfo->id);
				Session::set("userInfo",$userInfo);
				if($_POST["autologin"] == 1){//勾选自动登录
					//把用户名和密码记录cookies
					Cookie::set("email",$userEmail,36000000);
					Cookie::set("password",$userPassword,36000000);
				}
				unset($userEmail,$userPassword);
				$this->redirect("index","focus");
			}else{
				$this->error(L("errorlogin"));
			}
			 
		}
		
		public function _register(){
			$this->error(L("noregister"));
		}
		
		//用户注册
		public function register(){
			$this->display("register");
		}
		
		//找回密码
		public function getpassword(){
			$this->display();
		}
		
		//发送修改链接到信箱
		public function sendpassword(){
			$email = safeRequest($_POST["email"]);
			$record = D("user")->find("email=".$email,"id");
			if($record){
				import("ORG.PHPMailer.PHPMailer");
				import("ORG.PHPMailer.SMTP");
				import("ORG.PHPMailer.PHPMailerUtil");
				$md5 = md5(uniqid(rand(), true));
				$url = '请点击下面的链接修改密码<br/><a href="http://demo.guangla.com'.__APP__.'/login/changepassword/g/'.$md5.'">点此修改密码</a><br/>逛啦 (guangla.com)<br/><p></div></div>';
				if(PHPMailerUtil::send("guangla系统消息", $email, "密码修改", $url,true)){
					$this->success("密码修改链接已发送至你的邮箱".$email);
				}
			}else{
				$this->error(L("noemail"));
			}
		}
		
		public function _insert(){
			$this->error(L("noregister"));
		}
		
		//用户注册
		public function insert(){
			if(isset($_POST["email"])){
				$userDao = D("user");
				$userVo = $userDao->create();
				if($userVo){
					$userId = $userDao->add();
					$userVo->id = $userId;
					$usercontact = D("user_contact");
					$usercontact->userid = $userId;
					$usercontact->add();
					$userinfo = D("user_info");
					$userinfo->userid = $userId;
					$userinfo->add();
					Session::set("userInfo",$userVo);
					makeuserCache($userId); //用户cache初始化
					F("userInfo",$userDao->data,-1,getCachePath(APP_PATH."/UserCache",$userId));
					addFeed($userId,"addnetwork","加入了网络".$userVo->network_city);
					$this->redirect("index","focus");
				}else{
					$this->error($userDao->getError());
				}
			}else{
				$this->redirect("register","login");
			}
		}
		
		public function invite(){
			$u = safeRequest($_GET["u"]);
			$g = safeRequest($_GET["g"],1);
			$record = D("user_invite")->find("userid=".$u." and md5='".$g."'");
			if($record){
				$this->assign("record",$record);
				$this->display();
			}else{
				$this->error(L("noinvitelink"));
			}
		}

		public function saveinvite(){
			$dao = D("user_invite");
			$friendid = safeRequest($_POST["friendid"]);
			if($dao->delete("userid=".$friendid." and md5='".safeRequest($_POST["md5"],1)."'")){
				if(isset($_POST["email"])){
					$userDao = D("user");
					$userVo = $userDao->create();
					if($userVo){
						$userId = $userDao->add();
						$userVo->id = $userId;
						$usercontact = D("user_contact");
						$usercontact->userid = $userId;
						$usercontact->add();
						$userinfo = D("user_info");
						$userinfo->userid = $userId;
						$userinfo->add();
						Session::set("userInfo",$userVo);
						F("userInfo",$userVo,DATA_PATH.$userId."/");//写入cache	
						makeuserCache($userId); //用户cache初始化
						addFeed($userId,"addnetwork","加入了网络".$userVo->network_city);
						$friend = D("friends");
						$data[0]["userid"]   =  $userId;
						$data[0]["friendid"] =  $friendid;
						$data[0]["lasttime"] =  time();
						$data[1]["userid"]   =  $friendid;
						$data[1]["friendid"] =  $userId;
						$data[1]["lasttime"] =  time();
						$id= $friend->addAll($data);//添加好友	
						D("cache")->setField("friendCount","(friendCount+1)","userid=".$userId);
						D("cache")->setField("friendCount","(friendCount+1)","userid=".$friendid);
						addNotify($friendid,$userId,"checkfriend",$id,0);   //通知
						addFeed($userId,"friend",$friendid,0,$friendid); //动态
						addFeed($friendid,"friend",$userId,0,$userId); //动态
						$this->redirect("face","setting");
					}else{
						$this->error($userDao->getError());
					}
				}else{
					$this->redirect("index","index");
				}
			}else{
				$this->error(L("noinvitelink"));
			}
		}
		public function loginout(){	
			Session::clear();
			Cookie::clear();
			$this->redirect("index","index");
		}
		
	}
?>